package com.yao.controller;

import com.alibaba.fastjson.JSONObject;
import com.yao.domain.Test;
import com.yao.domain.User;
import com.yao.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author : hzyao
 * @date : 17:41 2021/7/27
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private TestService testService;
    @Resource
    private RedisTemplate redisTemplate;

    private final Logger LOG = LoggerFactory.getLogger(TestController.class);

    @GetMapping("/hello")
    public String hello() {
        return "Hello World!" ;
    }


    @GetMapping("/list")
    public List<Test> hello1() {
        return testService.list() ;
    }

    @RequestMapping("/redis/set/{key}/{value}")
    public String set(@PathVariable Long key, @PathVariable String value) {
        redisTemplate.opsForValue().set(key, value, 3600, TimeUnit.SECONDS);
        LOG.info("key：{}，value：{}", key, value);
        return "success";
    }

    @RequestMapping("/redis/get/{key}")
    public Object get(@PathVariable Long key) {
        Object value = redisTemplate.opsForValue().get(key);
        LOG.info("key：{}，value：{}", key, value);
        System.out.println(JSONObject.toJSON(value));
        //解析获取User.name
        User user = JSONObject.parseObject(value.toString(),User.class);
        System.out.println(user.getName());
        return value;
    }




}
